﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using E2_WEBSITE;

public partial class addtocart : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        int iDaLogin = (Int32)Session["IsLogin"];
        if (iDaLogin == 0)
        {
            Response.Redirect("/Common/Login.aspx");
            return;
        }


        int memID;  // user's id
        if (!Int32.TryParse(Session["UserID"].ToString(), out memID))
        {
            Response.Redirect("/Common/Login.aspx");
            return;
        }

        if (Request.QueryString["num"] == null)
        {
            if (Request.QueryString["pID"] != null
                && Request.QueryString["cID"] != null
                && Request.QueryString["quan"] != null)
            {
                int bID;    // current's basket id
                int pID;    // product id
                int cID;    // product's image id
                int quan;   // quantity

                if (Int32.TryParse(Request.QueryString["pID"], out pID)
                    && Int32.TryParse(Request.QueryString["cID"], out cID)
                    && Int32.TryParse(Request.QueryString["quan"], out quan))
                {
                    bID = Basket.GlobalBasket.getCurrentBasketID(memID);
                    if (bID == -1)  // if there's no basket
                    {
                        Basket.GlobalBasket.addNewBasket(memID);
                        bID = Basket.GlobalBasket.getCurrentBasketID(memID);
                    }

                    Basket.GlobalBasket.addToBasket(bID, pID, cID, quan);
                    Response.Redirect("/cp/member/member.aspx");
                }
            }
        }
        else
        {
            int num;
            if (Int32.TryParse(Request.QueryString["num"], out num))
            {
                bool bOK = true;
                for (int i = 1; i <= num; i++)
                {
                    if (!(Request.QueryString["pID" + i.ToString()] != null
                        && Request.QueryString["cID" + i.ToString()] != null
                        && Request.QueryString["quan" + i.ToString()] != null))
                    {
                        bOK = false;
                    }
                }
                if (!bOK)
                {
                    Response.Redirect("/default.aspx");
                    return;
                }
                for (int i = 1; i <= num; i++)
                {
                    if (Request.QueryString["pID" + i.ToString()] != null
                        && Request.QueryString["cID" + i.ToString()] != null
                        && Request.QueryString["quan" + i.ToString()] != null)
                    {
                        int bID;    // current's basket id
                        int pID;    // product id
                        int cID;    // product's image id
                        int quan;   // quantity

                        if (Int32.TryParse(Request.QueryString["pID" + i.ToString()], out pID)
                            && Int32.TryParse(Request.QueryString["cID" + i.ToString()], out cID)
                            && Int32.TryParse(Request.QueryString["quan" + i.ToString()], out quan))
                        {
                            bID = Basket.GlobalBasket.getCurrentBasketID(memID);
                            if (bID == -1)  // if there's no basket
                            {
                                Basket.GlobalBasket.addNewBasket(memID);
                                bID = Basket.GlobalBasket.getCurrentBasketID(memID);
                            }

                            Basket.GlobalBasket.addToBasket(bID, pID, cID, quan);
                        }
                    }
                }
                Response.Redirect("/cp/member/member.aspx");
            }
        }
    }
}